package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

import com.alicp.jetcache.anno.config.EnableMethodCache;
import com.example.demo.listen.RedisMsgPubSubListener;

@Configuration
@EnableMethodCache(basePackages = "org.example.demo")
public class JetcacheConfig {

//	@Bean(name = "defaultClient")
//	@DependsOn(RedisLettuceAutoConfiguration.AUTO_INIT_BEAN_NAME)
//	public LettuceFactory defaultClient() {
//		return new LettuceFactory("remote.default", RedisClient.class);
//	}

//	 @Bean
//     public MessageListenerAdapter listener(Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer,
//             MessageSubscriber subscriber) {
//         MessageListenerAdapter adapter = new MessageListenerAdapter(subscriber, "onMessage");
//         adapter.setSerializer(jackson2JsonRedisSerializer);
//         adapter.afterPropertiesSet();
//         return adapter;
//     }
//	 
//	 @Bean
//     public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
//             MessageListenerAdapter listener) {
//
//         RedisMessageListenerContainer container = new RedisMessageListenerContainer();
//         container.setConnectionFactory(connectionFactory);
//         container.addMessageListener(listener, new PatternTopic("/redis/*"));
//         return container;
//     }
	@Bean
	public GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer() {
		return new GenericJackson2JsonRedisSerializer();
	}

	@Bean
	public RedisTemplate redisTemplate(GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer,
			RedisConnectionFactory jedisConnectionFactory) {
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(jedisConnectionFactory);
		// redisTemplate.setKeySerializer(stringRedisKeySerializer);
		redisTemplate.setDefaultSerializer(genericJackson2JsonRedisSerializer);
		return redisTemplate;
	}

	@Bean
	RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
			MessageListenerAdapter listenerAdapter) {

		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		container.addMessageListener(listenerAdapter, new PatternTopic("/redis/pubsub"));

		return container;
	}

	@Bean
	MessageListenerAdapter listenerAdapter(RedisMsgPubSubListener listener) {
		MessageListenerAdapter adapter = new MessageListenerAdapter(listener, "onMessage");
		// Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer,
		// adapter.setSerializer(jackson2JsonRedisSerializer);
		adapter.afterPropertiesSet();
		return adapter;
	}

}
